(function () {
    if (window !== window.top)
        return;

    var origin           = location.origin;
    var nativeAutomation = {{{nativeAutomation}}};

    if (nativeAutomation)
        origin = {{{domain}}};

    var testRunId                                       = {{{testRunId}}};
    var browserId                                       = {{{browserId}}};
    var activeWindowId                                  = {{{activeWindowId}}};
    var windowId                                        = {{{windowId}}};
    var experimentalMultipleWindows                     = {{{experimentalMultipleWindows}}};
    var selectorTimeout                                 = {{{selectorTimeout}}};
    var pageLoadTimeout                                 = {{{pageLoadTimeout}}};
    var childWindowReadyTimeout                         = {{{childWindowReadyTimeout}}};
    var retryTestPages                                  = {{{retryTestPages}}};
    var speed                                           = {{{speed}}};
    var browserHeartbeatUrl                             = origin + {{{browserHeartbeatRelativeUrl}}};
    var browserStatusUrl                                = origin + {{{browserStatusRelativeUrl}}};
    var browserStatusDoneUrl                            = origin + {{{browserStatusDoneRelativeUrl}}};
    var browserIdleUrl                                  = origin + {{{browserIdleRelativeUrl}}};
    var browserOpenFileProtocolUrl                      = origin + {{{browserOpenFileProtocolRelativeUrl}}};
    var browserActiveWindowIdUrl                        = origin + {{{browserActiveWindowIdUrl}}};
    var browserActiveWindowIdUrl                        = origin + {{{browserActiveWindowIdUrl}}};
    var browserEnsureWindowInNativeAutomationUrl        = origin + {{{browserEnsureWindowInNativeAutomationUrl}}};
    var browserCloseWindowUrl                           = origin + {{{browserCloseWindowUrl}}};
    var browserDispatchNativeAutomationEventUrl         = origin + {{{browserDispatchNativeAutomationEventRelativeUrl}}};
    var browserDispatchNativeAutomationEventSequenceUrl = origin + {{{browserDispatchNativeAutomationEventSequenceRelativeUrl}}};
    var browserParseSelectorUrl                         = origin + {{{browserParseSelectorUrl}}};
    var skipJsErrors                                    = {{{skipJsErrors}}};
    var dialogHandler                                   = {{{dialogHandler}}};
    var userAgent                                       = {{{userAgent}}};
    var fixtureName                                     = {{{fixtureName}}};
    var testName                                        = {{{testName}}};
    var canUseDefaultWindowActions                      = {{{canUseDefaultWindowActions}}};

    var ClientDriver = window['%testCafeDriver%'];
    var driver       = new ClientDriver(testRunId,
        {
            heartbeat:                             browserHeartbeatUrl,
            status:                                browserStatusUrl,
            statusDone:                            browserStatusDoneUrl,
            idle:                                  browserIdleUrl,
            activeWindowId:                        browserActiveWindowIdUrl,
            ensureWindowInNativeAutomationUrl:     browserEnsureWindowInNativeAutomationUrl,
            closeWindow:                           browserCloseWindowUrl,
            openFileProtocolUrl:                   browserOpenFileProtocolUrl,
            dispatchNativeAutomationEvent:         browserDispatchNativeAutomationEventUrl,
            dispatchNativeAutomationEventSequence: browserDispatchNativeAutomationEventSequenceUrl,
            parseSelector:                         browserParseSelectorUrl,
        },
        {
            userAgent:      userAgent,
            fixtureName:    fixtureName,
            testName:       testName,
            activeWindowId: activeWindowId,
            windowId:       windowId,
        },
        {
            selectorTimeout:             selectorTimeout,
            pageLoadTimeout:             pageLoadTimeout,
            childWindowReadyTimeout:     childWindowReadyTimeout,
            skipJsErrors:                skipJsErrors,
            dialogHandler:               dialogHandler,
            retryTestPages:              retryTestPages,
            speed:                       speed,
            canUseDefaultWindowActions:  canUseDefaultWindowActions,
            nativeAutomation:            nativeAutomation,
            experimentalMultipleWindows: experimentalMultipleWindows,
        }
    );

    Object.defineProperty(window, '%testCafeDriverInstance%', {
        configurable: true,
        value:        driver
    });

    driver.start();
})();
